package com.hdoit.kylin.logical.document.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.hdoit.kylin.logical.document.dao.IReportCataLogDao;
import com.hdoit.kylin.logical.document.model.ReportCataLog;
import com.hdoit.kylin.logical.document.model.ReportCataLogEmployee;
import com.hdoit.kylin.logical.document.service.IReportCataLogEmployeeService;
import com.hdoit.kylin.logical.document.service.IReportCataLogService;
import com.hdoit.kylin.logical.viewmodel.document.ReportCataLogSearch;

/**
 * @author guoshuren
 * @email
 * @createtime 2018年02月01日 20:11:10
 * @comment
 */
@Component
public class ReportCataLogServiceImpl extends SuperService<ReportCataLog>
		implements IReportCataLogService {

	private IReportCataLogDao reportCataLogDao;
	private IReportCataLogEmployeeService reportCataLogEmployeeService;

	public IReportCataLogEmployeeService getReportCataLogEmployeeService() {
		return reportCataLogEmployeeService;
	}

	@Resource
	public void setReportCataLogEmployeeService(
			IReportCataLogEmployeeService reportCataLogEmployeeService) {
		this.reportCataLogEmployeeService = reportCataLogEmployeeService;
	}

	public IReportCataLogDao getReportCataLogDao() {
		return reportCataLogDao;
	}

	@Resource
	public void setReportCataLogDao(IReportCataLogDao reportCataLogDao) {
		this.reportCataLogDao = reportCataLogDao;
	}

	@Override
	public List<ReportCataLog> queryList(ReportCataLogSearch search,
			PagerModel pager) {
		// TODO Auto-generated method stub
		return reportCataLogDao.queryList(search, pager);
	}

	@Override
	public List<ReportCataLog> queryListByTeacherId(String teacherId) {
		// TODO Auto-generated method stub
		return reportCataLogDao.queryListByTeacherId(teacherId);
	}

	@Override
	public boolean applyChange(ReportCataLog reportCataLog) {
		ReportCataLogEmployee reportCataLogEmployee = new ReportCataLogEmployee();

		if (reportCataLog.getShareIds()!=null && !reportCataLog.getShareIds().isEmpty()) {
			String[] shareIdList = reportCataLog.getShareIds().split(",");

			if (ModelStatus.update.equals(reportCataLog.getModelStatus())) {
				reportCataLogEmployeeService.deleteByReportId(reportCataLog
						.getId());
			}
			reportCataLogEmployee.setReportCataLogId(reportCataLog.getId());
			for (String shareId : shareIdList) {
				reportCataLogEmployee.setId(CommonUtils.getKeyId());
				reportCataLogEmployee.setEmployeeId(shareId);
				if (!reportCataLogEmployeeService
						.applyChange(reportCataLogEmployee)) {
					throw new DaoBaseRuntimeException(
							"apply shareIds roll back!!");
				}

			}
		}
		if (!super.applyChange(reportCataLog)) {
			throw new DaoBaseRuntimeException("apply reportCataLog roll back");
		}

		return true;

	}

	@Override
	public List<ReportCataLog> queryListByReportCataLogIds(
			List<String> reportIds) {
		return getReportCataLogDao().queryListByReportCataLogIds(reportIds);
	}

}